<?php

/**
 * 菜单处理
 */
namespace app\system\model;

class SystemApplicationModel {

    private $count = 0;

    /**
     * 获取应用列表
     */
    public function loadList() {
        $list = glob(APP_PATH . '*/config/config.php');
        $configArray = array();
        foreach ($list as $file) {
            $this->count++;
            $file = str_replace('\\', '/', $file);
            $fileName = str_replace(APP_PATH, '', $file);
            $fileName = explode('/', $fileName);
            $fileName = $fileName[0];
            $info = $this->getInfo($fileName);
            $configArray[$info['app']] = $info;
        }
        return $configArray;
    }

    /**
     * 应用数量
     * @return int
     */
    public function countList() {
        return $this->count;
    }

    /**
     * 应用信息
     * @param $app
     * @return array|mixed
     */
    public function getInfo($app) {
        $info = load_config(APP_PATH . $app . '/config/config.php');
        if (empty($info)) {
            return array();
        }
        $info['app'] = $app;
        if ($info['app.system']) {
            $info['app.state'] = 1;
            $info['app.install'] = 1;
        }
        return $info;
    }

    /**
     * 应用ID列表
     */
    public function getIds() {
        $appList = $this->loadList();
        $appIds = array();
        foreach ($appList as $vo) {
            $appIds[] = $vo['app.id'];
        }
        return $appIds;
    }

}
